package com.android.itiraf;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class Database extends SQLiteOpenHelper {

	private static final int VERSION = 1;
	private static String[] SELECT = {"id", "category", "desc"};
	private static String WHERE = "category like ?";
	String table ="itiraf";
	public Database (Context con,String DbName){
		super(con,DbName,null,VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		db.execSQL("CREATE TABLE "+table+" (id INTEGER PRIMARY KEY AUTOINCREMENT,category TEXT,desc Text);");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		db.execSQL("DROP TABLE IF EXIST "+table+" ;");
		onCreate(db);
	}
	public void fillArrayList(Cursor cursor,ArrayList<String> list){
        int i =0;
		while(cursor.moveToNext()){

        long id = cursor.getLong(cursor.getColumnIndex("id"));
        String ctgry = cursor.getString((cursor.getColumnIndex("category")));
        String desc = cursor.getString((cursor.getColumnIndex("desc")));
        
        list.add(i++,ctgry);
        list.add(i++,desc);
        
        }
		   
}
	public void writeDB(String title, String desc){
		 
	     SQLiteDatabase db = getWritableDatabase();
	     ContentValues cv = new ContentValues();
	     cv.put("category", title);
	     cv.put("desc",desc);
	     db.insertOrThrow(table, null, cv);
	     }
	
	public void clear(){
		SQLiteDatabase db = getWritableDatabase();
		db.delete(table, null, null);
		 }
	 
   public Cursor getRows(String [] clause){
  	     SQLiteDatabase db = getReadableDatabase();
  	     Cursor cursor = db.query(table, SELECT,WHERE, clause, null, null, null);
  	     return cursor;
  	     }
}
